//
//  TTBeautyManagerSearchController.m
//  TaTaBang
//
//  Created by 李禹 on 15/8/30.
//  Copyright (c) 2015年 hailong. All rights reserved.
//

#import "TTBeautyManagerSearchController.h"
#import "NJSettingTextFieldItem.h"
#import "NJSettingGroup.h"
#import "NJProductCell.h"
#import "NJSettingArrowItem.h"
#import "TTBeautyManagerSearchResultController.h"

@interface TTBeautyManagerSearchController ()

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation TTBeautyManagerSearchController

- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        // 第一组数据
        NJSettingItem *item00 = [[NJSettingTextFieldItem alloc] initWithTitle:@"手机号" placeholder:@"请输入手机号"];
                
        NJSettingGroup *group1 = [[NJSettingGroup alloc] init];
        group1.items = @[item00] ;
        
        _dataArray = [NSMutableArray array];
        [_dataArray addObject:group1];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"查找美容服务";
    
//    self.view.backgroundColor = [UIColor lightGrayColor];

    [self setUI];
    
}

- (void)setUI {
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 90)];
    UIButton *commitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [commitButton setBackgroundImage:[UIImage resizableImageWithName:@"btn_background"] forState:UIControlStateNormal];
    [commitButton setBackgroundImage:[UIImage resizableImageWithName:@"btn_background"] forState:UIControlStateHighlighted];
    [commitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [commitButton setTitle:@"查找" forState:UIControlStateNormal];
    [commitButton addTarget:self action:@selector(doSearch:) forControlEvents:UIControlEventTouchUpInside];
    commitButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    commitButton.frame = CGRectMake(22, 20, SCREEN_WIDTH - 44, 50);
    [footerView addSubview:commitButton];
    self.tableView.tableFooterView = footerView;
}

- (id)init {
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (id)initWithStyle:(UITableViewStyle)style {
    return [super initWithStyle:UITableViewStyleGrouped];
}

#pragma mark - UIButton Action Method
- (void)doSearch:(UIButton *)btn {
    TTBeautyManagerSearchResultController *resultVc = [[TTBeautyManagerSearchResultController alloc] init];
    [self.navigationController pushViewController:resultVc animated:YES];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // 先取出对应组的小数组
    NJSettingGroup *g = self.dataArray[section];
    // 返回小数组的长度
    return g.items.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 1.创建cell
    NJProductCell *cell = [NJProductCell cellWithTableView:tableView];
    
    // 先取出对应组的组模型
    NJSettingGroup *g = self.dataArray[indexPath.section];
    //  从组模型中取出对应行的模型
    NJSettingItem *item = g.items[indexPath.row];
    cell.item = item;
    
    // 3.返回cell
    return cell;
}

- (CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        return 80;
    }else {
        return 44;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 立即取消选中
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 先取出对应组的组模型
    NJSettingGroup *g = self.dataArray[indexPath.section];
    //  从组模型中取出对应行的模型
    NJSettingItem *item = g.items[indexPath.row];
    // 判断block中是否保存了代码
    if (item.option != nil) {
        // 如果保存,就执行block中保存的代码
        item.option();
    }else if ([item isKindOfClass:[NJSettingArrowItem class]]) {
        
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    // 先取出对应组的组模型
    NJSettingGroup *g = self.dataArray[section];
    return g.headerTitle;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    // 先取出对应组的组模型
    NJSettingGroup *g = self.dataArray[section];
    return g.footerTitle;
}

@end
